package com.eazyimage.lab.controller;

import com.eazyimage.lab.service.FileUtils;
import com.eazyimage.lab.entity.Response;
import com.eazyimage.lab.service.DatasetMessService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.zip.ZipOutputStream;

import static com.eazyimage.lab.service.SH256Service.getSHA256Str;


@RestController
public class DatasetDownloadController {
    @Autowired
    DatasetMessService service;

    @RequestMapping("/datasetdownload")
    public Response datasetdownload(HttpServletResponse response, @RequestParam("datasetid") String datasetid) {
        ZipOutputStream zos = null;
        String exdatasetid = getSHA256Str(datasetid).toString();
        String zipname = "/easyimage/frontend/dist/ziptemp/"+exdatasetid+".zip";
        try{
            File inFile = new File("/easyimage/backend-Flask/static/dataset/" + datasetid);
            zos = new ZipOutputStream(new FileOutputStream(zipname), Charset.forName("GBK"));
            zos.setComment("多文件处理");
            FileUtils.zipFile(inFile, zos, "");
        } catch (IOException e){
            e.printStackTrace();
            return Response.fail();
        } finally {
            if(zos!=null){
                try {
                    zos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        Thread sleepDel = new Thread(new FileUtils.RHDel(zipname));
        sleepDel.start();

        return Response.success("http://116.62.23.69/ziptemp/"+exdatasetid+".zip");
    }

}




















